<?php

namespace backend\controllers;
use Yii;
use backend\models\AdminRole;
use backend\models\Admin_Node;
use yii\data\Pagination;

/*
*角色管理
 */
class RoleController extends \yii\web\Controller
{
	public $layout = 'admin_main';
    function init()
    {
        $session = Yii::$app->session;
        if(empty($session->get('admin_user')))
        {
            $this->redirect('?w=login/login');
        }
    }
	//展示角色
    public function actionRole_lists()
    {
    	$Role = new AdminRole();
    	$data = $Role->findRole();
        $NodeData = Admin_Node::find()->asArray()->all();
        $nowPage = Yii::$app->request->get('page') ? Yii::$app->request->get('page') : 1;
    	return $this->render('Role',
            ['role_data'=> $data['role_data'],
            'RoleForm' => $Role ,
            'NodeData' => $NodeData,
            'page' => $data['page'],
            'nowPage' => $nowPage
            ]);
    }

    //添加角色
    public function actionAdd_role()
    {
        
        $Role = new AdminRole();
        $data = Yii::$app->request->post();
        $res = $Role->roleAdd($data);
        if ($res) {
            $this->redirect(['role_lists']);   
        }else{
            exit('添加失败!');
        }
    }

    //查询一条
    public function actionFindone()
    {
        $id = Yii::$app->request->post('id') ? Yii::$app->request->post('id') : 1;
        $obj = new AdminRole;
        return $obj->roleOne($id);
    }

    //修改
    public function actionUpdate_role()
    {
        $obj = new AdminRole;
        $data = Yii::$app->request->post();
        if($obj->roleUpdate($data['AdminRole'],$data['id'])){
            $this->redirect($_SERVER['HTTP_REFERER']);
        }else{
            exit('修改失败');
        }
    }

    //删除
    public function actionRole_delete()
    {
        $model = new AdminRole();
        $id = Yii::$app->request->post('role_id') ? Yii::$app->request->post('role_id') : 0;
        if ($id!=0) {
            if ($model->role_delete($id)) {
                $json['error'] = true;
                $json['msg']   = Yii::$app->request->post('url');
                return json_encode($json);
            }else
            {
                $json['error'] = false;
                $json['msg']   = '删除失败';
                return json_encode($json);
            }
        }else{
            $json['error'] = false;
            $json['msg']   = '删除失败';
            return json_encode($json);
        }
        
        // echo "<pre>";
        // print_r($data);
    }
}